package main

import (
	"encoding/json"
	"fmt"
)

/*
√ 将谦哥老婆的JSON信息转换为map
√ 将谦哥老婆的JSON信息转换为结构体
√ 将谦哥小姨子们的JSON信息转换为map切片
√ 将谦哥小姨子们的JSON信息转换为结构体切片
*/

var jsonStr = `{"Name":"于谦","Age":50,"Rmb":123.45,"Sex":true,"Hobby":["抽烟","喝酒","烫头"]}`

/*将谦哥的JSON信息转换为map*/
func main031() {
	jsonBytes := []byte(jsonStr)
	dataMap := make(map[string]interface{})
	err := json.Unmarshal(jsonBytes, &dataMap)
	if err != nil{
		fmt.Println("反序列化失败，err=",err)
		return
	}
	fmt.Println(dataMap)
}

/*将谦哥的JSON信息转换为结构体*/
func main032() {

	type Person struct {
		Name string
		Age int
		Rmb float64
		Sex bool
		Hobby []string
	}

	jsonBytes := []byte(jsonStr)
	personPtr := new(Person)

	err := json.Unmarshal(jsonBytes, personPtr)
	if err != nil{
		fmt.Println("反序列化失败，err=",err)
		return
	}
	fmt.Println(*personPtr)
}

/*将谦哥小姨子们的JSON信息转换为map切片*/
func main033() {
	jsonStr := `[{"hobby":["抽中华","喝牛栏山","烫花卷头"],"name":"王钢蛋"},{"hobby":["抽玉溪","喝五粮液","烫杀马特"],"name":"王铁蛋"},{"hobby":["抽小熊猫","喝剑南春","烫鸡冠头"],"name":"王铜蛋"}]`
	jsonBytes := []byte(jsonStr)
	dataSlice := make([]map[string]interface{}, 0)

	err := json.Unmarshal(jsonBytes, &dataSlice)
	if err != nil{
		fmt.Println("反序列化失败，err=",err)
		return
	}
	fmt.Println(dataSlice)
}

/*将谦哥小姨子们的JSON信息转换为结构体切片*/
func main034() {

	type Person struct {
		Name string
		Age int
		Rmb float64
		Sex bool
		Hobby []string
	}

	jsonStr := `[{"hobby":["抽中华","喝牛栏山","烫花卷头"],"name":"王钢蛋"},{"hobby":["抽玉溪","喝五粮液","烫杀马特"],"name":"王铁蛋"},{"hobby":["抽小熊猫","喝剑南春","烫鸡冠头"],"name":"王铜蛋"}]`
	jsonBytes := []byte(jsonStr)
	persons := make([]Person, 0)

	err := json.Unmarshal(jsonBytes, &persons)
	if err != nil{
		fmt.Println("反序列化失败，err=",err)
		return
	}
	fmt.Println(persons)
}
